<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="分类名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="默认不超过14字" :maxlength="14"></el-input>
      </el-form-item>
      <el-form-item label="分类描述" prop="description">
        <el-input class="w500" v-model="form.desc" placeholder="例如：描述"></el-input>
      </el-form-item>
      <el-form-item label="分类标识" prop="action">
        <el-input class="w500" v-model="form.action" placeholder="英文 唯一不可重复" disabled></el-input>
      </el-form-item>
      <el-form-item label="添加图片">
        <el-upload
          :action="upload_url"
          list-type="picture-card"
          :file-list="fileList"
          :on-preview="handlePictureCardPreview"
          :on-success="handleAvatarSuccess">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog v-model="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="选择所属应用">
        <el-select v-model="form.app_id" placeholder="请选择">
          <el-option v-for="item in applistall" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="可读的应用">
        <el-checkbox-group v-model="allow_app_read">
          <el-checkbox v-for="i in applistall" :label="i.id" :key="i.id">{{i.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="排序">
        <el-input class="w500" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'
  import { UPLOAD_URL } from '@/config/const'

  export default {
    data: function () {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        fileList: [
          {name: '', url: ''}
        ],
        allow_app_read: [],
        rules: {
          name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      ...mapState({
        applistall: state => state.app.all,
        cover: state => state.recommend.cover,
        form: state => state.recommend.recommendInfo
      }),
      upload_url () {
        return UPLOAD_URL
      }
    },
    async created () {
      await this.recommendInfo()
      this.fileList[0].name = String(this.form.name)
      this.fileList[0].url = String(this.form.profile_pic)
      let arr = this.form.other_app_read.split(',')
      for (let i = 0; i < arr.length; i++) {
        arr[i] = Number(arr[i])
      }
      this.allow_app_read = arr
      this.appAll()
    },
    methods: {
      // 获取推荐位详情
      async recommendInfo () {
        let payload = {
          id: this.$route.params.id
        }
        await this.$store.dispatch('recommend/recommendInfo', payload)
      },
      // 获取不分页的应用列表
      async appAll () {
        await this.$store.dispatch('app/appAll')
      },
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.form.other_app_read = this.allow_app_read.join(',')
            if (JSON.stringify(this.cover) !== '{}') {
              this.form.profile_pic = 'http://' + this.cover.save_path + '/' + this.cover.filename
            }
            this.$store.dispatch('recommend/recommendEdit', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/goods/recommend')
            })
          }
        })
      },
      handleAvatarSuccess (file, response, fileList) {
        const self = this
        if (file.code !== 200) {
          fileList.pop()
          self.$message({
            message: '封面上传失败',
            type: 'error'
          })
        } else {
          self.$message({
            message: '封面上传成功',
            type: 'success'
          })
        }
        self.$store.commit('recommend/RECOMMEND_COVER', file.res)
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
